package app.ui;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import database.local.Business;
import database.local.Deal;
import database.local.Utils;

public class OneTabListActivity extends Activity {
	private List<Business> list;
	private ListView lv1;
	BusinessListArrayAdapter blaa;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_tab_layout);
		
		//get extras 
		Bundle b = getIntent().getBundleExtra("values");
		list = b.getParcelableArrayList("list");
		

		
		//open the list
		lv1 = (ListView) findViewById(R.id.list_tab_list_list);
		
		createConcretAdapter();
		
		lv1.setAdapter(blaa);
		//lv1.setAdapter(new BusinessListArrayAdapter(this, list));
		lv1.setOnItemClickListener( new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> parentView, View childView, int position,
					long id) {
				openDealWindow(list.get(position), list.get(position).getDeals().get(0));
			}
		});
	
	}

	protected void openDealWindow(Business b, Deal d) {
		Intent openDealActivity = new Intent(this, DealViewActivity.class);
		Bundle bundle = new Bundle();
		
		bundle.putParcelable("deal", d);
		bundle.putParcelable("business", b);
		
		openDealActivity.putExtra("values", bundle);
		this.startActivity(openDealActivity);
		
	}
	
	/*-----------------------------------------Menu-----------------------------------------------------*/
	public boolean onCreateOptionsMenu(Menu menu) {
		//super.onCreateOptionsMenu(menu);
		
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.layout.list_menu, menu);
	    return true;
	}
	
	
	
	public boolean onOptionsItemSelected(MenuItem item) {
		//super.onOptionsItemSelected(item);
		
		switch (item.getItemId()){
		case R.id.list_menu_sort :
			openSortDialog();
			return true;
		case R.id.list_menu_map_return :
			
			Intent intent = new Intent();
			setResult(RESULT_CANCELED, intent);
			finish();
			return true;
		}
		
		return false;
	}

	private void openSortDialog() {
		final String[] items = new String[3];
		items[0] = "Rating";
		items[1] = "Distance";
		items[2] = "Name";
		
		new AlertDialog.Builder( this )
    	.setTitle( "Sort by" )
    	.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				sortList(items[which]);
			}
    	}
    			)
    	.show();	
	
		
		
		
		
		}
	
	protected void sortList(String string) {
		
		chooseSort(string);
		
		lv1.setAdapter(blaa);
		lv1.invalidate();		
	}

	private void chooseSort(String string) {
		if (string.equalsIgnoreCase("Rating")){
			Log.i("Sort", "Sorting by Rating");
			list = Utils.getListbyRating(list);
		}
		else if (string.equalsIgnoreCase("Distance")){
			Log.i("Sort", "Sorting by Distance");
			list = Utils.getListbyDistance(list);
		}
		else if (string.equalsIgnoreCase("Name")){
			Log.i("Sort", "Sorting by Name");
			list = Utils.getListbyName(list);
		}
	}

	/** Create adapter according to the type.
	 * @param type - type of the adapter to create pub/rest/all
	 */
	public void createConcretAdapter(){
			blaa = new BusinessListArrayAdapter(this, list);
	}
}
